package com.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.JiashuEntity;
import com.entity.view.JiashuView;
import com.service.JiashuService;
import com.service.TokenService;
import com.utils.MD5Util;
import com.utils.MPUtil;
import com.utils.PageUtils;
import com.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.annotation.IgnoreAuth;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/jiashu")
public class JiashuController {

        // 注入service
        @Autowired
        private JiashuService jiashuService;

        @Autowired
        private TokenService tokenService;

        /**
         * 注册
         */
        @IgnoreAuth
        @RequestMapping("/register")
        public R register(@RequestBody JiashuEntity jiashu) {
                JiashuEntity u = jiashuService.selectOne(new EntityWrapper<JiashuEntity>().eq("yonghuming", jiashu.getYonghuming()));
                if (u != null) return R.error("用户已存在");
                Long uId = new Date().getTime();
                jiashu.setId(uId);
                jiashu.setMima(MD5Util.md5(jiashu.getMima()));
                jiashuService.insert(jiashu);
                return R.ok();
        }

        /**
         * 登录
         */
        @IgnoreAuth
        @RequestMapping(value = "/login")
        public R login(String username, String password, String captcha, HttpServletRequest request) {
                JiashuEntity u =  jiashuService.selectOne(new EntityWrapper< JiashuEntity>().eq("yonghuming", username));
                if(u==null || !u.getMima().equals(MD5Util.md5(password))) {
                        return R.error("账号或密码不正确");
                }
                String token = tokenService.generateToken(u.getId(), username,"jiashu",  "家属" );
                return R.ok().put("token", token);
        }


        /**
         * 退出
         */

        public R logout(HttpServletRequest request) {
                request.getSession().invalidate();
                return R.ok("退出成功");
        }

        /**
         * 获取用户的session用户信息
         */
        @RequestMapping("/session")
        public R getCurrUser(HttpServletRequest request) {
                Long id = (Long)request.getSession().getAttribute("userId");
                JiashuEntity u = jiashuService.selectById(id);
                return R.ok().put("data", u);
        }


        /**
         * 密码重置
         */
        @IgnoreAuth
        @RequestMapping(value = "/resetPass")
        public R resetPass(String username, HttpServletRequest request) {
                JiashuEntity u = jiashuService.selectOne(new EntityWrapper<JiashuEntity>().eq("yonghuming", username));
                if(u==null) {
                        return R.error("账号不存在");
                }
                u.setMima(MD5Util.md5("123456"));
                jiashuService.updateById(u);
                return R.ok("密码重置成功,默认密码：123456");
        }


        /**
         * 后端列表
         */
        @RequestMapping("/page")
        public R page(@RequestParam Map<String, Object> params, JiashuEntity jiashu, HttpServletRequest request) {
                EntityWrapper<JiashuEntity> ew = new EntityWrapper<JiashuEntity>();

                PageUtils page = jiashuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, jiashu), params), params));

                return R.ok().put("data", page);
        }


        /**
         * 列表
         */

        @RequestMapping("/lists")
        public R list( JiashuEntity jiashu){
                EntityWrapper<JiashuEntity> ew = new EntityWrapper<JiashuEntity>();
                ew.allEq(MPUtil.allEQMapPre( jiashu, "jiashu"));
                return R.ok().put("data", jiashuService.selectListView(ew));
        }

        /**
         * 查询
         */
        @RequestMapping("/query")
        public R query(JiashuEntity jiashu){
                EntityWrapper< JiashuEntity> ew = new EntityWrapper< JiashuEntity>();
                ew.allEq(MPUtil.allEQMapPre( jiashu, "jiashu"));
                JiashuView jiashuView =  jiashuService.selectView(ew);
                return R.ok("查询家属成功").put("data", jiashuView);
        }

        /**
         * 家属详情
         */
        @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") String id){
                JiashuEntity jiashu = jiashuService.selectById(id);
                return R.ok().put("data", jiashu);
        }

        /**
         * 保存
         */
        @RequestMapping("/save")
        public R save(@RequestBody JiashuEntity jiashu, HttpServletRequest request){
                jiashu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
                JiashuEntity u = jiashuService.selectOne(new EntityWrapper<JiashuEntity>().eq("yonghuming",jiashu.getYonghuming()));
                if(u!=null){
                        return R.error("用户已经存在");
                }
                jiashu.setId(new Date().getTime());
                jiashu.setMima(MD5Util.md5(jiashu.getMima()));
                jiashuService.insert(jiashu);
                return R.ok();
        }

        /**
         * 修改
         */
        @RequestMapping("/update")
        @Transactional
        public R update(@RequestBody JiashuEntity jiashu, HttpServletRequest request){
                //ValidatorUtils.validateEntity(laorenzinv);
                JiashuEntity JiashuEntity = jiashuService.selectById(jiashu.getId());
                if(StringUtils.isNotBlank(jiashu.getMima()) && !jiashu.getMima().equals(JiashuEntity.getMima())) {
                        jiashu.setMima(MD5Util.md5(jiashu.getMima()));
                }
                jiashuService.updateById(jiashu);//全部更新
                return R.ok();
        }
}
